package com.utf999.qiangmanger.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.AbstractRelationalDBDAO;
import com.utf999.qiangmanger.dao.iface.IAdminUserDAO;
import com.utf999.qiangmanger.entity.orm.AdminUserEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;

/**
 * @author yinwenjie
 *
 */
@Component("AdminUserDAOImpl")
public class AdminUserDAOImpl extends AbstractRelationalDBDAO<AdminUserEntity> implements IAdminUserDAO {

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.AbstractRelationalDBDAO#getEntityClass()
	 */
	@Override
	protected Class<AdminUserEntity> getEntityClass() {
		return AdminUserEntity.class;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IAdminUserDAO#queryAdminUser(java.lang.String)
	 */
	//TODO 数据库未进行索引优化
	@Override
	public AdminUserEntity queryAdminUser(String userName) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("userName", userName);
		
		List<AdminUserEntity> users = null;
		try {
			users = this.queryByHqlFile("IAdminUserDAO.queryAdminUser", condition);
		} catch (Exception e) {
			 throw new BizException(e.getMessage(), ResponseCode.E501);
		}
		if(users == null || users.size() == 0) {
			return null;
		}
		
		return users.get(0);
	}
	
	//TODO 数据库未进行索引优化
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IAdminUserDAO#queryAdminByStore(java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<AdminUserEntity> queryAdminByStore(String storeid , Integer status) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("storeid", storeid);
		if(status != null) {
			condition.put("status", status);
		}
		
		List<AdminUserEntity> users = this.queryByHqlFile("IAdminUserDAO.queryAdminByStore", condition);
		return users;
	}
	
	//TODO 数据库未进行索引优化
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IAdminUserDAO#querySuperAdmin(java.lang.Integer)
	 */
	@Override
	public List<AdminUserEntity> querySuperAdmin(Integer status) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		if(status != null) {
			condition.put("status", status);
		}
		List<AdminUserEntity> users = this.queryByHqlFile("IAdminUserDAO.querySuperAdmin", condition);
		return users;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IAdminUserDAO#queryCompanyAdmin(java.lang.String)
	 */
	@Override
	public List<AdminUserEntity> queryCompanyAdmin(String companyid) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyuid", companyid);
		return this.queryByHqlFile("IAdminUserDAO.queryCompanyAdmin", condition);
	}
}
